package com.ql.mangersystem.service.impl;

import com.ql.mangersystem.common.ErrorCode;
import com.ql.mangersystem.common.Page;
import com.ql.mangersystem.common.Result;
import com.ql.mangersystem.entity.dto.LogsPageDTO;
import com.ql.mangersystem.entity.po.Logs;
import com.ql.mangersystem.exception.ServiceException;
import com.ql.mangersystem.mapper.LogsMapper;
import com.ql.mangersystem.service.LogsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LogsServiceImpl implements LogsService {
    @Resource
    private LogsMapper logsMapper;

    @Override
    public void save(Logs logs) {
        String ip = logs.getIp();
        String user = logs.getUser();
        String type = logs.getType();
        String operation = logs.getOperation();
        String time = logs.getTime();
        int result = logsMapper.insert(ip,user,type,operation,time);
        if (result <=0) {
            throw new ServiceException(ErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public int delete(Integer id) {

        return logsMapper.delete(id);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        return logsMapper.deleteBatch(ids);
    }

    @Override
    public Page<Logs> page(LogsPageDTO logsPageDTO) {
        String operation = logsPageDTO.getOperation();
        Integer pageNum = logsPageDTO.getPageNum();
        Integer pageSize = logsPageDTO.getPageSize();
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<Logs> page = new Page<>();
        if (operation==null){
            List<Logs> list = logsMapper.page(skipNum,pageSize);
            page.setList(list);
            Integer count =logsMapper.count();
            page.setTotal(count);
//            System.out.println(operation+":null");
//            System.out.println(page);
            return page;
        }
        List<Logs> list =logsMapper.pageLike(operation,skipNum,pageSize);
        page.setList(list);
        Integer count = logsMapper.countLike(operation);
        page.setTotal(count);
        return page;
    }
}
